package com.letao.service.impl;

import com.letao.common.utils.JsonUtils;
import com.letao.mapper.TbItemParamItemMapper;
import com.letao.pojo.TbItemParamItem;
import com.letao.pojo.TbItemParamItemExample;
import com.letao.service.ItemParamItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/1/11.
 */
@Service
public class ItemParamItemServiceImpl implements ItemParamItemService {
    @Autowired
    private TbItemParamItemMapper itemParamItemMapper;

    @Override
    public String getParaHtmlByItemId(Long itemId) {
        TbItemParamItemExample example = new TbItemParamItemExample();
        example.createCriteria().andItemIdEqualTo(itemId);
        List<TbItemParamItem> paramItems = itemParamItemMapper.selectByExampleWithBLOBs(example);
        if(paramItems == null || paramItems.size() == 0){
            return null;
        }
        //封装html字符串
        TbItemParamItem tbItemParamItem = paramItems.get(0);
        String paramData = tbItemParamItem.getParamData();

        List<Map> list = JsonUtils.jsonToList(paramData, Map.class);

        StringBuffer sb = new StringBuffer();
        sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">");
        sb.append("    <tbody>");
        for(Map m1:list) {
            sb.append("        <tr>");
            sb.append("            <th class=\"tdTitle\" colspan=\"2\">"+m1.get("group")+"</th>");
            sb.append("        </tr>");
            List<Map> list2 = (List<Map>) m1.get("params");
            for(Map m2:list2) {
                sb.append("        <tr>");
                sb.append("            <td class=\"tdTitle\">"+m2.get("k")+"</td>");
                sb.append("            <td>"+m2.get("v")+"</td>");
                sb.append("        </tr>");
            }
        }
        sb.append("    </tbody>");
        sb.append("</table>");
        return sb.toString();
    }
}
